package xyz.xiaoshengfu.xrpc.admin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>Title: LoginInterceptor</p>
 * <p>Description: 用户登陆拦截器</p>
 * @author xiaoshengfu(2439323118@qq.com)
 * @date 2020年3月25日
 */
public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Boolean login = (Boolean) request.getSession().getAttribute("adminLogin");
		if (login == null || !login) {
			response.sendRedirect("/xrpc-admin/login_page");
			return false;
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// handler执行之后，返回ModeAndView之前
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// 完成处理，返回ModelAndView之后。可以再此处理异常
	}

}
